package com.mc.book.controller;

import cn.hutool.core.util.StrUtil;
import com.mc.book.constant.Constant;
import com.mc.book.enums.ResponseEnum;
import com.mc.book.pojo.Result;
import com.mc.book.pojo.request.RequestAI;
import com.mc.book.pojo.request.RequestBook;
import com.mc.book.pojo.resp.RespAI;
import com.mc.book.service.BookService;
import com.mc.book.utils.baidu.BaiduText;
import com.mc.book.utils.baidu.WenXinUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@Api(tags = "AI模块")
@RestController
@RequestMapping("/mc/ai")
public class AIApi extends BaseApi {
    private static final Logger log = LoggerFactory.getLogger(AIApi.class);
    @Autowired
    private BookService bookService;

    @ApiOperation("简单AI交流")
    @PostMapping("/simple")
    public Result<RespAI> simple(@RequestBody RequestAI requestAI) {
        try {
            RequestBook requestBook = new RequestBook();
            requestBook.setId(requestAI.getBookId());
            Result<RequestBook> bookById = bookService.getBookById(requestBook);
            RequestBook book = bookById.getData();
            if (book == null) {
                return Result.fail(ResponseEnum.BOOK_NULL.getCode(), ResponseEnum.BOOK_NULL.getMessage());
            }
            RespAI respAI = new RespAI();
            respAI.setBookId(requestAI.getBookId());
            String content = requestAI.getContent();
            if (StrUtil.isEmpty(content)) {
                String speech = requestAI.getSpeech();
                if (StrUtil.isEmpty(speech)) {
                    return Result.raw(respAI);
                }
                content = BaiduText.getContentNow(speech);
                if (Objects.equals(content, "识别失败！")) {
                    return Result.fail(ResponseEnum.AI_ERROR.getCode(), ResponseEnum.AI_ERROR.getMessage());
                }
            }
            respAI.setContent(content);
            respAI.setRespContent(WenXinUtil.chat(StrUtil.format(Constant.SIMPLE_AI_REQ, book.getBook_name(), content)));
            return Result.raw(respAI);
        } catch (Exception e) {
            log.error("系统异常：", e);
            return Result.fail(ResponseEnum.UP_SYSTEM_FAILURE.getCode(), ResponseEnum.UP_SYSTEM_FAILURE.getMessage());
        }
    }

    @ApiOperation("简单AI交流-流式响应")
    @PostMapping(value = "/simpleStream", produces = "text/event-stream;charset=UTF-8")
    public void simpleStream(@RequestBody RequestAI requestAI, HttpServletResponse response) {
        try {
            RequestBook requestBook = new RequestBook();
            requestBook.setId(requestAI.getBookId());
            Result<RequestBook> bookById = bookService.getBookById(requestBook);
            RequestBook book = bookById.getData();
            if (book == null) {
                Result.fail(ResponseEnum.BOOK_NULL.getCode(), ResponseEnum.BOOK_NULL.getMessage());
            }
            RespAI respAI = new RespAI();
            respAI.setBookId(requestAI.getBookId());
            String content = requestAI.getContent();
            if (StrUtil.isEmpty(content)) {
                String speech = requestAI.getSpeech();
                if (StrUtil.isEmpty(speech)) {
                    Result.raw(respAI);
                }
                content = BaiduText.getContentNow(speech);
                if (Objects.equals(content, "识别失败！")) {
                    Result.fail(ResponseEnum.AI_ERROR.getCode(), ResponseEnum.AI_ERROR.getMessage());
                }
            }
            respAI.setContent(content);
            WenXinUtil.chatStream(StrUtil.format(Constant.SIMPLE_AI_REQ, book.getBook_name(), content), response);
        } catch (Exception e) {
            log.error("系统异常：", e);
            Result.fail(ResponseEnum.UP_SYSTEM_FAILURE.getCode(), ResponseEnum.UP_SYSTEM_FAILURE.getMessage());
        }
    }

    @ApiOperation("简单AI交流-流式响应")
    @GetMapping(value = "/simpleStream", produces = "text/event-stream;charset=UTF-8")
    public void simpleStreamGet(@RequestParam("bookId") Integer bookId, @RequestParam String content, HttpServletResponse response) {
        RequestAI requestAI = new RequestAI();
        requestAI.setBookId(bookId);
        requestAI.setContent(content);
        simpleStream(requestAI, response);
    }


}
